var $,
    layer,
    form,
    upload,
    table;

layui.use(['jquery', 'layer', 'table', 'form', 'upload'], function () {
    $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        upload = layui.upload,
        table = layui.table;

    function updateOnline(id, online){
        var user =JSON.parse(localStorage.getItem('user'));
        $.ajax({
            url: '/UserCoupon/updateUserCoupon',
            type: 'post',
            dataType: 'json',
            data: {
                'couponId': id,
                'online': online,
                'updateId': user.userId,
            },
            success: function (item) {
               if (item.data){
                   getUseCouponUsedData();
               };
            }
        });
    };
});


//查询用户信息
getUseCouponUsedData();
function getUseCouponUsedData(account, nickname){
    $.ajax({
        url: '/UserCouponUsed/getAllTable',
        type: 'post',
        dataType: 'json',
        data: {
            'account': account,
            'nickname': nickname
        },
        success: function (item) {
            getUseCouponUsedTable(item.data);
        }
    });
};

//展示用户表
function getUseCouponUsedTable(data) {
    table.render({
        elem: '#demo',
        data: data,
        toolbar: true,
        page:{//自定义分页
            layout:['count','prev','page','next','limit','skip','refresh'],
            limits:[3,6,9],//分页选项
            first:"首页",
            last:"尾页",
        },
        cols: [[
            {type: 'checkbox'},
            {field:'userId',title:'用户ID', align:'center'},
            {field:'account',title:'用户账号',align:'center'},
            {field:'nickname',title:'用户名称',align:'center'},
            {field:'useDate',title:'已使用',align:'center'},
            {field:'getDate',title:'未使用',align:'center'},
            {field:'expire',title:'已过期',align:'center'},
            {title:'操作',align:'center', templet:function(data){
                    return "<a href='JavaScript:addUseCouponUsed(\"" + data.userId +  "\");' class='operation'>赠送优惠券</a>";
                }},
        ]]
    });
};

//赠送优惠券
function addUseCouponUsed(id) {
    layer.open({
        type: 2,
        maxmin: 'true',
        offset: 'auto',
        resize: false,
        title: '赠送优惠券',
        area: ['1000px', '600px'],
        content:'/coupon/layer/layer-userCoupons-addUseCouponUsed.html?tx=' + encodeURI(id)
    });
}

//搜索
function search () {
    var data = form.val('searchForm');
    getUseCouponUsedData(data.account, data.nickname)
};

//刷新
function refresh () {
    location.reload();
};

function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);;
};